package response

import (
	"goweb/web/global"

	"github.com/gofiber/fiber/v2"
)

// 定义请求响应结构体
type Response struct {
	Code int         `json:"code"` // 状态码
	Msg  string      `json:"msg"`  // 提示信息
	Data interface{} `json:"data"` // 响应数据
}

// 将数据转换成Json格式
func Result(code int, data interface{}, msg string, c *fiber.Ctx) error {
	return c.JSON(Response{
		Code: code,
		Msg:  msg,
		Data: data,
	})
}

// 默认成功请求为 200
func Success(c *fiber.Ctx) error {
	return Result(global.StatusOK, map[string]interface{}{}, "请求成功", c)
}

func SuccessWithCode(code int, c *fiber.Ctx) error {
	return Result(code, map[string]interface{}{}, "请求成功", c)
}

func SuccessWithData(data interface{}, c *fiber.Ctx) error {
	return Result(global.StatusOK, data, "请求成功", c)
}

func SuccessWithMessage(msg string, c *fiber.Ctx) error {
	return Result(global.StatusOK, map[string]interface{}{}, msg, c)
}

// 默认失败请求为 400
func Fail(c *fiber.Ctx) error {
	return Result(global.StatusBadRequest, map[string]interface{}{}, "请求失败", c)
}

func FailWithCode(code int, c *fiber.Ctx) error {
	return Result(code, map[string]interface{}{}, "请求失败", c)
}

func FailWithData(data interface{}, c *fiber.Ctx) error {
	return Result(global.StatusBadRequest, data, "请求失败", c)
}

func FailWithMessage(msg string, c *fiber.Ctx) error {
	return Result(global.StatusBadRequest, map[string]interface{}{}, msg, c)
}
